define(["moment"], function (moment) {
    return ["$scope", "$filter", "$calculater", "$mdDialog", "$stateParams", "config", "MMWService", '$datePicker',
    function ($scope, $filter, $calculater, $mdDialog, $stateParams, config, $MMWService, $datePicker) {
		var run_count = 0;
		var has_lock = false;
		$scope.init = function(){
			//每隻程式都有，等於window.onload方法
			$scope.temp_url_for_lot = "module/moduleA/program/A05/A05_lot.html";
			$scope.temp_url_for_op = "module/moduleA/program/A05/A05_op.html";
			$scope.temp_url_for_equipment = "module/moduleA/program/A05/A05_equipment.html";
			
			$scope.select_all_lot_bool = false;
			$scope.lot_info_data = [];
			$scope.op_info_data = [];
			$scope.equipment_info_data = [];
			
			$scope.has_data = [];
			$scope.lot_is_click = "";
			$scope.op_is_click = "";
			$scope.add_equipment_data = {};
			$scope.add_equipment_data.equipment_no = "";
			$scope.add_equipment_data.equipment_name ="";
			$scope.add_equipment_data.dis_qty = 0;
			$scope.add_equipment_data.work_date = new Date();

			
			run_count = 0;
			has_lock = false;
			//first for show and push selet data
			load_lot_data(function(lot_data)
			{
				$scope.lot_info_data = lot_data;
				if($scope.lot_info_data.length > 0)
				{
					get_data_by_lot(lot_data[0],true,false);
				}
			});

			// 20201211 modify by 外包 for M#83345: 派工底稿：任务字段查询、可排设备过滤只显示可用设备。
			$scope.order_by_mode = {name:"",sort:false};
			$scope.order_by_eq_mode = {name:"",sort:false};
			$scope.order_by_lot_mode = {name:"",sort:false};
			$scope.dispatch_filter = {};
		}
		
		$scope.back = function(){
            //返回首頁，傳入true表示使用退出
            $scope.$root.hideMenu();
            $scope.$root.changeProgram('home', true);//動畫
		}
		
		// 20201211 modify by 外包 for M#83345: 派工底稿：任务字段查询、可排设备过滤只显示可用设备。
		$scope.orderbychange = function(mode){
			if($scope.order_by_mode.name != mode)
			{
				$scope.order_by_mode.name = mode;
				$scope.order_by_mode.sort = false;
			}
			else
			{
				if ($scope.order_by_mode.sort)
					$scope.order_by_mode.sort = false;
				else
					$scope.order_by_mode.sort = true;
			}
		}

		// 20201211 modify by 外包 for M#83345: 派工底稿：任务字段查询、可排设备过滤只显示可用设备。
		$scope.orderbyEqchange = function(mode){
			if($scope.order_by_eq_mode.name != mode)
			{
				$scope.order_by_eq_mode.name = mode;
				$scope.order_by_eq_mode.sort = false;
			}
			else
			{
				if ($scope.order_by_eq_mode.sort)
					$scope.order_by_eq_mode.sort = false;
				else
					$scope.order_by_eq_mode.sort = true;
			}
		}

		// 20201211 modify by 外包 for M#83345: 派工底稿：任务字段查询、可排设备过滤只显示可用设备。
		$scope.orderbyLotchange = function(mode){
			if($scope.order_by_lot_mode.name != mode)
			{
				$scope.order_by_lot_mode.name = mode;
				$scope.order_by_lot_mode.sort = false;
			}
			else
			{
				if ($scope.order_by_lot_mode.sort)
					$scope.order_by_lot_mode.sort = false;
				else
					$scope.order_by_lot_mode.sort = true;
			}
		}

		// 20201211 modify by 外包 for M#83345: 派工底稿：任务字段查询、可排设备过滤只显示可用设备。
		$scope.filterOpnoInfo = function(list, filterItem){
			if (list) {
				if ($scope.order_by_mode.name != "")
					list = $filter('orderBy')(list, $scope.order_by_mode.name, $scope.order_by_mode.sort);
				var temp_list = list.filter(function (item, index, array) {
					var isOK = true,
						keys = Object.keys(filterItem);
					for (var i = 0; i < keys.length; i++) {
						var key = keys[i];
						if (isOK && item[key] != null && filterItem[key] && filterItem[key] != '') {
							if (item[key].toString().indexOf(filterItem[key]) == -1) {
								isOK = false;
								break;
							}
						}
					}
					// 20230327 modify by Alan for#131406 : 不顯示op_seq == '9999' 的設備
					if(item.op_seq == '9999') {
						return false;
					}
					return isOK;
				});
				return temp_list;
			} else {
				return list;
			}
		}

		// 20201211 modify by 外包 for M#83345: 派工底稿：任务字段查询、可排设备过滤只显示可用设备。
		$scope.filterEquipmentInfo = function(list, filterItem){
			if (list) {
				if ($scope.order_by_eq_mode.name != "")
					if($scope.order_by_eq_mode.name == 'dis_qty'){
						list.forEach(element => {
							element.dis_qty = parseInt(element.dis_qty)
						});
					}
					list = $filter('orderBy')(list, $scope.order_by_eq_mode.name, $scope.order_by_eq_mode.sort);
				var temp_list = list.filter(function (item, index, array) {
					var isOK = true,
						keys = Object.keys(filterItem);
					for (var i = 0; i < keys.length; i++) {
						var key = keys[i];
						if (isOK && item[key] != null && filterItem[key] && filterItem[key] != '') {
							if (item[key].toString().indexOf(filterItem[key]) == -1) {
								isOK = false;
								break;
							}
						}
					}
					return isOK;
				});
				return temp_list;
			} else {
				return list;
			}
		}

		// 20201211 modify by 外包 for M#83345: 派工底稿：任务字段查询、可排设备过滤只显示可用设备。
		$scope.filterLotInfo = function(list, filterItem){
			if (list) {
				if ($scope.order_by_lot_mode.name != "")
					list = $filter('orderBy')(list, $scope.order_by_lot_mode.name, $scope.order_by_lot_mode.sort);
				var temp_list = list.filter(function (item, index, array) {
					var isOK = true,
						keys = Object.keys(filterItem);
					for (var i = 0; i < keys.length; i++) {
						var key = keys[i];
						if (isOK && item[key] != null && filterItem[key] && filterItem[key] != '') {
							if (item[key].toString().indexOf(filterItem[key]) == -1) {
								isOK = false;
								break;
							}
						}
					}
					return isOK;
				});
				return temp_list;
			} else {
				return list;
			}
		}

		$scope.confirm = function(){
			//確認按鈕
			var need_push_data = $filter('filter')($scope.lot_info_data,{is_select:"1"});
			var submit_list = [];
			for(var i in need_push_data)
			{
				var is_has_data = $filter('filter')($scope.has_data,{lot_no:need_push_data[i].lot_no});
				if(is_has_data.length > 0)
				{
					var has_op_info = is_has_data[0].op_info;
					for(var j in has_op_info)
					{
						var has_equipment_info = has_op_info[j].equipment_info.data;
						if(has_equipment_info.length > 0)
						{
							var push_obj = {};
							push_obj.lot_no = need_push_data[i].lot_no;
							push_obj.mo_no = need_push_data[i].mo_no;
							push_obj.lot_qty = need_push_data[i].lot_qty;
							push_obj.process_no =  need_push_data[i].process_no;
							push_obj.process_ver =  need_push_data[i].process_ver;
							push_obj.op_no = has_op_info[j].op_no;
							push_obj.op_qty = has_op_info[j].op_qty;
							push_obj.equipment_info = has_equipment_info;
							submit_list.push(push_obj);
						}
					}
				}
				else
				{
					var push_obj = {};
					push_obj.lot_no = need_push_data[i].lot_no;
					push_obj.mo_no = need_push_data[i].mo_no;
					push_obj.lot_qty = need_push_data[i].lot_qty;
					push_obj.process_no =  need_push_data[i].process_no;
					push_obj.process_ver =  need_push_data[i].process_ver;
					push_obj.op_no = "";
					push_obj.op_qty = 0;
					push_obj.equipment_info = [];
					submit_list.push(push_obj);
				}
			}
			if(submit_list.length > 0)
			{
				//console.log(submit_list);
				var content = {};
				content.aps_data = submit_list;
				$MMWService.sendToServer({
					uri: 'ServicesSTD.Module_WIP.aps_dispatch_paper_process',
					content : content,
					success : function(data)
					{
						$scope.init();
						$scope.$root.showAlert($filter('translate')('A05.js.success'));
					}
				});
			}
		}
		$scope.add_equipment = function()
		{
			var can_open_qty = 0
			var is_select = "0";
			var lot_info = $filter('filter')($scope.has_data,{lot_no:$scope.lot_is_click});
			if(lot_info.length > 0)
			{
				is_select = lot_info[0].is_select;
				var op_info = $filter('filter')(lot_info[0].op_info,{op_no:$scope.op_is_click})
				if(op_info.length > 0)
				{
					can_open_qty =  op_info[0].equipment_info.can_dis_sum_qty;
				}
			}
			if(can_open_qty > 0 && is_select == "1")
				$mdDialog.dialog('module/moduleA/program/A05/A05_search.html',equipment_dialog_controller);
			else
			{
				if(is_select == "0")
					$scope.$root.showAlert($filter('translate')('A05.js.tick_lot'));
				else
					$scope.$root.showAlert($filter('translate')('A05.js.can_not_add_dis'));	
			}
				
		}

		$scope.changeDate = function (equipment_item) {
		    $datePicker.open(equipment_item.work_date,
            function (dialog) {
                equipment_item.work_date = moment(dialog.datePacker.pickDate).format('YYYY-MM-DD');
                dialog.hide();
            },
            function (dialog) {
                dialog.hide();
            }, {});
		};
		function equipment_dialog_controller(dialog)
		{
			var controller = 
			{
				now_date: new Date(),
				add_equipment_data:$scope.add_equipment_data,
				init:function()
				{
					$scope.add_equipment_data.equipment_no = "";
					$scope.add_equipment_data.equipment_name = "";
					$scope.add_equipment_data.dis_qty = 0;
					$scope.add_equipment_data.work_date = new Date();		
				},
				loadEquipment:function()
				{
					$MMWService.pageNumberInfoGet({
						page_mode : 'aps_dispatch_equipment_info',
						query_condition_info : [
							// 20201211 modify by 外包 for M#83345: 派工底稿：任务字段查询、可排设备过滤只显示可用设备。
							{ column_name:'WLB.BaseLotNo', query_mode:'0', value:$scope.lot_is_click},
							{ column_name:'POA.OPNo', query_mode:'0', value:$scope.op_is_click}
						]
					}, function (response) {
						$scope.$root.showSelect({
							title: $filter('translate')('A05.js.equipment_no'),
							label:
							function(item){
								return item.equipmentno + ' ('+ item.equipmentname +')';
							},
							code: 'equipmentno',
							selectCode:$scope.add_equipment_no,
							list: response,
							confirm: function (item,select_dialog) {
								select_dialog.hide();
								$scope.add_equipment_data.equipment_no= item.equipmentno;
								$scope.add_equipment_data.equipment_name = item.equipmentname;
							}
						});
					});
				},
				equipmentQtyAdd :function(event)
				{
					var target = event.currentTarget;
					angular.element(target).addClass('is-focus');
					$calculater.open(function(num,calcDialog)
					{ 
						angular.element(target).removeClass('is-focus');
						calcDialog.hide();
						var lot_info = $filter('filter')($scope.has_data,{lot_no:$scope.lot_is_click});
						if(lot_info.length > 0)
						{
							
							var op_info = $filter('filter')(lot_info[0].op_info,{op_no:$scope.op_is_click});
							if(op_info.length > 0)
							{
								var can_dis_sum_qty = op_info[0].equipment_info.can_dis_sum_qty;
								if(can_dis_sum_qty < num)
									$scope.$root.showAlert($filter('translate')('A05.js.qty_over'));
								else
									$scope.add_equipment_data.dis_qty = num;
							}
							else
							$scope.$root.showAlert($filter('translate')('A05.js.can_not_add_op'));	
						}
						else
							$scope.$root.showAlert($filter('translate')('A05.js.can_not_add_lot')); 
        			},
					function(calcDialog)
					{
    					angular.element(target).removeClass('is-focus');
    					calcDialog.hide();
    				},
    				{
    					defaultNum : $scope.add_equipment_data.dis_qty,
    					decimal : false
    				});
				},
				back: function () 
				{
					dialog.hide();
				},
				confirm: function () 
				{
					//console.log($scope.add_equipment_data);
					$scope.add_equipment_data.work_date = moment($scope.add_equipment_data.work_date).format('YYYY-MM-DD');
					if(!$scope.add_equipment_data.equipment_no)
					{
						dialog.hide();
						$scope.$root.showAlert($filter('translate')('A05.js.not_setting_add_eq'));
						return;
					}
					if(!$scope.add_equipment_data.work_date)
					{
						dialog.hide();
						$scope.$root.showAlert($filter('translate')('A05.js.not_setting_add_date'));
						return;
					}
					if($scope.add_equipment_data.dis_qty <= 0)
					{
						dialog.hide();
						$scope.$root.showAlert($filter('translate')('A05.js.not_setting_add_qty'));
						return;
					}

					var lot_info = $filter('filter')($scope.has_data,{lot_no:$scope.lot_is_click});
					if(lot_info.length > 0)
					{
						var op_info = $filter('filter')(lot_info[0].op_info,{op_no:$scope.op_is_click});
						//console.log(op_info);
						if(op_info.length > 0)
						{
							var equipment_info = op_info[0].equipment_info;
							//console.log(equipment_info);
							var eq_condition = {};
							eq_condition.equipment_no = $scope.add_equipment_data.equipment_no;
							eq_condition.work_date = $scope.add_equipment_data.work_date;
							var has_data = $filter('filter')(equipment_info.data,eq_condition);
							if(parseFloat($scope.add_equipment_data.dis_qty) > equipment_info.can_dis_sum_qty)
							{
								dialog.hide();
								$scope.$root.showAlert($filter('translate')('A05.js.can_not_add_dis'));
								return;
							}
							equipment_info.can_dis_sum_qty = equipment_info.can_dis_sum_qty - parseFloat($scope.add_equipment_data.dis_qty);
							if(has_data.length > 0)
							{
								has_data[0].dis_qty = parseFloat(has_data[0].dis_qty) +parseFloat($scope.add_equipment_data.dis_qty);
							}
							else
							{
								var obj = {};
								obj.equipment_name = $scope.add_equipment_data.equipment_name;
								obj.aps_qty = 0;
								obj.op_no = $scope.op_is_click;
								obj.has_qty = 0;
								obj.work_date = $scope.add_equipment_data.work_date;
								obj.equipment_no = $scope.add_equipment_data.equipment_no;
								obj.dis_qty = $scope.add_equipment_data.dis_qty;
								obj.has_mark = "1";
								equipment_info.data.push(obj);
							}
						}
					}
					dialog.hide();
					
				}
			};
			return controller;
		}
		$scope.select_all_lot_action = function()
		{
			if($scope.select_all_lot_bool)
			{
				$scope.select_all_lot_bool = false;
				for(var i in $scope.lot_info_data )
				{
					$scope.lot_info_data[i].is_select = "0";	
				}
				for(var i in $scope.has_data)
				{
					var lot_item = $scope.has_data[i];
					for(var j in lot_item.op_info)
					{
						var op_item = lot_item.op_info[j];
						for(var k in op_item.equipment_info.data)
						{
							var equipment_item = op_item.equipment_info.data[k];
							equipment_item.dis_qty = 0;
						}
					}
				}
			}
			else
			{
				$scope.select_all_lot_bool = true;
				var has_been_run = [];
				var has_count = $scope.has_data.length;
				for(var i in $scope.lot_info_data )
				{
					$scope.lot_info_data[i].is_select = "1";
					var has_data = $filter('filter')($scope.has_data,{lot_no:$scope.lot_info_data[i].lot_no});
					if(has_data.length <= 0)
					{
						has_been_run.push($scope.lot_info_data[i]);
					}
					else
					{
						has_data[0].is_select = "1";
					}
				}
				calculate_dis_qty_action();
				run_count = has_been_run.length;
				if(run_count > 0)
				{
					has_lock = true;
					for(var i in has_been_run)
					{
						get_data_by_lot_forloop(has_been_run[i]);
					}
				}	
			}
		}
		$scope.select_lot_action = function(event,index,lot_item)
		{
			if(lot_item.is_select == "0")
			{
				lot_item.is_select ="1";
			}
			else
			{
				lot_item.is_select ="0";
			}
			var is_select_data = $filter('filter')($scope.has_data,{lot_no:lot_item.lot_no});
			if(is_select_data.length > 0)
			{
				is_select_data[0].is_select = lot_item.is_select;
				if(lot_item.is_select == "0")
				{
					for(var i in is_select_data[0].op_info)
					{
						var op_item = is_select_data[0].op_info[i];
						for(var j in op_item.equipment_info.data)
						{
							var equipment_item = op_item.equipment_info.data[j];
								equipment_item.dis_qty = 0;
						}
					}
				}
			}	
			get_data_by_lot(lot_item,false,true);
		}
		//歸零派工數量
		$scope.clear_dis_qty = function(event,equipment,equipment_item)
		{
			equipment.can_dis_sum_qty += parseFloat(equipment_item.dis_qty);
			equipment_item.has_mark = "1";
			equipment_item.dis_qty = 0;
		}
		//修改派工數量
		$scope.change_dis_qty_action = function(event,equipment,equipment_item)
		{
			var current_data = $filter('filter')($scope.has_data,{is_select:"1",lot_no:$scope.lot_is_click});
			//console.log(current_data);
			if(current_data.length > 0)
			{
				var target = event.currentTarget;
				angular.element(target).addClass('is-focus');
				$calculater.open(function(num,calcDialog)
				{ 
					angular.element(target).removeClass('is-focus');
					calcDialog.hide();
					equipment.can_dis_sum_qty += parseFloat(equipment_item.dis_qty);
					if(equipment.can_dis_sum_qty - num  < 0)
					{
						equipment.can_dis_sum_qty -= parseFloat(equipment_item.dis_qty);
						$scope.$root.showAlert($filter('translate')('A05.js.qty_over'));
					}
					else
					{
						equipment.can_dis_sum_qty =  equipment.can_dis_sum_qty - num;
						equipment_item.dis_qty = num;
						equipment_item.has_mark = "1";
						//calculate_dis_qty_action();
					}	
				},
				function(calcDialog)
				{
					angular.element(target).removeClass('is-focus');
					calcDialog.hide();
				},
				{
					defaultNum : equipment_item.dis_qty,
					decimal : false
				});
			}
			else
				$scope.$root.showAlert($filter('translate')('A05.js.tick_lot'));
				//$filter('translate')('A05.js.can_not_add_dis')
		}
		//切換生產批
		$scope.change_lot_action = function(event,index,lot_item)
		{
			get_data_by_lot(lot_item,true,false);
		}
		//切換作業站
		$scope.change_op_action= function(event,index,op_item)
		{
			var lot_data = $filter('filter')($scope.has_data,{lot_no:op_item.lot_no});
			$scope.op_is_click = op_item.op_no;
			$scope.equipment_info_data = op_item.equipment_info;
		}
		//取得該選擇生產批的所有資料(作業站 取全部 設備依該作業站的第一筆取全部)
		function get_data_by_lot(lot_item,is_change,is_calculate)
		{
			var has_data =  $filter('filter')($scope.has_data,{lot_no:lot_item.lot_no});
			if(has_lock)
				return;
			if(has_data.length <= 0)
			{
				load_op_and_equipment_data(lot_item,function(op_and_equipment_data)
				{
					if(op_and_equipment_data.length > 0)
					{
						var obj = {};
						obj.source_type = lot_item.source_type;
						obj.source_status = lot_item.source_status;
						obj.mo_no = lot_item.mo_no;
						obj.lot_no = lot_item.lot_no;
						obj.product_no = lot_item.product_no;
						obj.product_name = lot_item.product_name;
						obj.item_spec = lot_item.item_spec;
						obj.plan_start_date = lot_item.plan_start_date;
						obj.plan_end_date = lot_item.plan_end_date;
						obj.lot_qty = lot_item.lot_qty;
						obj.process_no = lot_item.process_no;
						obj.process_ver = lot_item.process_ver;
						obj.is_select = lot_item.is_select;
						obj.op_info = op_and_equipment_data;
						$scope.has_data.push(obj);
						$scope.has_data = $filter('orderBy')($scope.has_data, 'lot_no');
						if(is_change)
						{
							$scope.lot_is_click = lot_item.lot_no;
							$scope.op_info_data = op_and_equipment_data;
							$scope.op_is_click = op_and_equipment_data[0].op_no;
							$scope.equipment_info_data = op_and_equipment_data[0].equipment_info;
						}
						if(is_calculate)
							calculate_dis_qty_action();
						
						
					}
				});
			}
			else
			{
				if(is_change)
				{
					$scope.lot_is_click = lot_item.lot_no;
					$scope.op_info_data = has_data[0].op_info;
					$scope.op_is_click = has_data[0].op_info[0].op_no;
					$scope.equipment_info_data = has_data[0].op_info[0].equipment_info;
				}
				if(is_calculate)
					calculate_dis_qty_action();
			}

		}
		//取得生產批資料 BY 迴圈
		function get_data_by_lot_forloop(lot_item)
		{
			load_op_and_equipment_data(lot_item,function(op_and_equipment_data){
				if(op_and_equipment_data.length > 0)
				{
					var obj = {};
						obj.source_type = lot_item.source_type;
						obj.source_status = lot_item.source_status;
						obj.mo_no = lot_item.mo_no;
						obj.lot_no = lot_item.lot_no;
						obj.product_no = lot_item.product_no;
						obj.product_name = lot_item.product_name;
						obj.item_spec = lot_item.item_spec;
						obj.plan_start_date = lot_item.plan_start_date;
						obj.plan_end_date = lot_item.plan_end_date;
						obj.lot_qty = lot_item.lot_qty;
						obj.process_no = lot_item.process_no;
						obj.process_ver = lot_item.process_ver;
						obj.is_select = "1";
						obj.op_info = op_and_equipment_data;
						$scope.has_data.push(obj);	
				}
				run_count--;
				if(run_count <= 0)
				{
					has_lock = false;
					$scope.has_data = $filter('orderBy')($scope.has_data, 'lot_no');
					calculate_dis_qty_action();
				}
			});
		} 
		//計算派工量
		function calculate_dis_qty_action()
		{
			console.log($scope.has_data);
			var diff_condition = [];
			
			for(var i in $scope.has_data)
			{
				if($scope.has_data[i].is_select == "1")
				{
					var condition = {};
					condition.mo_no = $scope.has_data[i].mo_no;
					condition.process_no = $scope.has_data[i].process_no;
					condition.process_ver = $scope.has_data[i].process_ver;
					condition.is_select = "1";
					if($filter('filter')(diff_condition,condition).length <= 0)
					{
						diff_condition.push(condition);
					}
				}	
				
			}
			//console.log(diff_condition);
			//計算預設派工數量
			var diff_equipment_obj = [];
			for(var i in diff_condition)
			{
				var lot_info = $filter('filter')($scope.has_data,diff_condition[i]);
				for(var j in lot_info)
				{
					var lot_item = lot_info[j];
					
					for(var k in lot_item.op_info)
					{
						
						var op_item =lot_item.op_info[k];
						var sum_dis_qty = 0;
						var lot_qty = 0+parseFloat(lot_item.lot_qty);
						//console.log(op_item.op_no+":"+lot_qty);
						//console.log(op_item.equipment_info.can_dis_sum_qty);
						for(var l in op_item.equipment_info.data)
						{
							var equipment_item = op_item.equipment_info.data[l];
							var lot_gap = 0;
							var condition = {};
							condition.equipment_no = equipment_item.equipment_no;
							condition.work_date = equipment_item.work_date;
							condition.mo_no = lot_item.mo_no;
							condition.op_no = op_item.op_no;
							condition.process_no = lot_item.process_no;
							condition.process_ver = lot_item.process_ver;

							//console.log(lot_item.lot_no+":"+equipment_item.op_no+":"+equipment_item.equipment_no+":"+lot_qty+":"+equipment_item.aps_qty);
							if(equipment_item.has_mark == "1")
							{
								var lot_gap = lot_qty - parseFloat(equipment_item.dis_qty);
								if($filter('filter')(diff_equipment_obj,condition).length > 0)
								{
									var eq_obj = $filter('filter')(diff_equipment_obj,condition)[0];
									var aps_gap = parseFloat(eq_obj.aps_qty) - parseFloat(equipment_item.dis_qty);
									if(aps_gap > 0)
										eq_obj.aps_qty = aps_gap;
									else
										eq_obj.aps_qty = 0;
								}
								else
								{
									var eq_obj = {};
									eq_obj.equipment_no = equipment_item.equipment_no;
									eq_obj.work_date = equipment_item.work_date;
									eq_obj.mo_no = lot_item.mo_no;
									eq_obj.op_no = op_item.op_no;
									eq_obj.process_no = lot_item.process_no;
									eq_obj.process_ver = lot_item.process_ver;
									var aps_gap = parseFloat(equipment_item.aps_qty) - parseFloat(equipment_item.dis_qty);
									if(aps_gap > 0)
										eq_obj.aps_qty = aps_gap;
									else
										eq_obj.aps_qty = 0;
									diff_equipment_obj.push(eq_obj);
								}
							}
							else
							{
								
								if($filter('filter')(diff_equipment_obj,condition).length > 0)
								{
									var eq_obj = $filter('filter')(diff_equipment_obj,condition)[0];
									var aps_gap = parseFloat(eq_obj.aps_qty) - lot_qty;
									lot_gap = lot_qty - parseFloat(eq_obj.aps_qty);
									if(aps_gap > 0)
									{
										eq_obj.aps_qty = aps_gap;
										equipment_item.dis_qty = 0 + lot_qty;
									}
									else
									{
										equipment_item.dis_qty = 0 + parseFloat(eq_obj.aps_qty);
										eq_obj.aps_qty = 0;
									}
								}
								else
								{
									var eq_obj = {};
									eq_obj.equipment_no = equipment_item.equipment_no;
									eq_obj.work_date = equipment_item.work_date;
									eq_obj.mo_no = lot_item.mo_no;
									eq_obj.op_no = op_item.op_no;
									eq_obj.process_no = lot_item.process_no;
									eq_obj.process_ver = lot_item.process_ver;
									var aps_gap = parseFloat(equipment_item.aps_qty) - lot_qty;
									lot_gap = lot_qty - parseFloat(equipment_item.aps_qty);
									if(aps_gap > 0)
									{
										eq_obj.aps_qty = aps_gap;
										equipment_item.dis_qty = 0 + lot_qty;
									}
									else
									{
										equipment_item.dis_qty = 0 +parseFloat(equipment_item.aps_qty);
										eq_obj.aps_qty = 0;
									}
									diff_equipment_obj.push(eq_obj);
								}
							}
							if(lot_gap > 0)
								lot_qty = lot_gap;
							else
								lot_qty = 0;
							sum_dis_qty += equipment_item.dis_qty;
						}
						var can_gap = op_item.equipment_info.op_sum_qty -sum_dis_qty;
						if(can_gap > 0)
							op_item.equipment_info.can_dis_sum_qty = can_gap;
						else
							op_item.equipment_info.can_dis_sum_qty  = 0;
					}
				}
			}
			//console.log(diff_equipment_obj);
		}
		function load_lot_data(feedback)
		{
			var content = {data_type:"0"};
			$MMWService.sendToServer({
				uri: 'ServicesSTD.Module_WIP.aps_dispatch_paper_info_get',
				content : content,
				success : function(data){
					
					feedback(data.return_data);
				}
			});
		}
		function load_op_and_equipment_data(condition,feedback)
		{
				var content = {data_type:"1",query_condition_for_op:condition};
				$MMWService.sendToServer({
					uri: 'ServicesSTD.Module_WIP.aps_dispatch_paper_info_get',
					content : content,
					success : function(data){
						feedback(data.op_and_eq);
						
					}
				});
		}
		/*function load_equipment_data(index,condition,feedback)
		{
			//console.log(condition);
			var content = {data_type:"2",query_condition_for_equipment:condition};
				$MMWService.sendToServer({
					uri: 'ServicesSTD.Module_WIP.aps_dispatch_paper_info_get',
					content : content,
					success : function(data)
					{
						feedback(index,data.return_data);
					}
				});
		}*/
		//sMES 2.1.0 以後新增
		/*$scope.final = function(callback){
			//當離開此程式的時候一定會執行此函式
			Callback();//結束程式
		}*/

	}
]});